unit cx_google;

interface

uses
cx_vars, cx_http, SysUtils, StrUtils, DateUtils;

const

{
cx_google_startkeyword='<font size=+1><b>';
cx_google_endkeyword='</b>';
}
cx_google_pre_startkeyword='<h2 class=r';
cx_google_startkeyword = '<b>';
cx_google_endkeyword='</b>';
//
cx_google_root = 'body';
cx_google_node = 'h2';
cx_google_data = 'b';

function Google_GetRate(CurrencyTo, CurrencyFrom:string): TRates;

implementation
uses
  Windows;
  {
  m_api,
  cx_XML;
  }


function Google_GetRate(CurrencyTo, CurrencyFrom:string): TRates;
var
    pricestr: string;
    start, finish,i : integer;
    {amount, }price : real;
    IsPrice : boolean;
    //
    response: String;
    //datastr: String;
    //response: WideString;
    //root, actnode, datanode: HXML;
    //tmp: PWideChar;
begin
  result.success:=false;
  //
    result.coming.rate:=0;
    result.coming.date:=cx_Tomorrow;
    if DaysBetween(result.current.date, cx_Today)<>0 then
     begin //Fuck you Spielberg!!!
      result.previous.rate:=result.current.rate;
      result.previous.date:=result.current.date;
     end;
    result.current.rate:=0;
    result.current.date:=cx_Today;
    //result.previous.rate:=0;
    //result.previous.date:=cx_DateFrom;

    DecimalSeparator:='.';
    //response:=HTTPGet('google.com',80,'/search?q=1+'+CurrencyTo+'+to+'+CurrencyFrom);
    response:=HTTP_NL_Get('http://www.google.com/search?q=1+'+CurrencyTo+'+to+'+CurrencyFrom);
    //MessageBox(0, PChar(WideCharToString(response)), piShortName, MB_OK or MB_ICONINFORMATION);
    {
    start:=pos(cx_google_startkeyword,response)+Length(cx_google_startkeyword);
    finish:=posex(cx_google_endkeyword,response,start)-start;
    }
    start:=pos(cx_google_pre_startkeyword,response)+Length(cx_google_pre_startkeyword);
    start:=posex(cx_google_startkeyword,response,start)+Length(cx_google_startkeyword);
    finish:=posex(cx_google_endkeyword,response,start)-start;
    //
    response:=trim(MidStr(response,start,finish));
    //ShowMessage(response); //debug
    response:=AnsiReplaceStr(response, ',','.');

    pricestr:='';
    IsPrice:=false;
    for i:=0 to length(response)-1 do
    begin
      if response[i]+response[i+1]='= ' then IsPrice:=true;
      if (response[i+2]=' ') and not (response[i-1]+response[i]='= ') then
            IsPrice:=false;

      if IsPrice then
        pricestr:=pricestr+response[i+2];

    end;
    DecimalSeparator:='.';
    price:=StrToFloat(pricestr);
    result.current.rate:=price;
    result.current.date:=cx_Today;
    result.success:=True;
    (*
    response:=response+'</body>';
    with cx_XMLw do
     begin
      try
       //root:=parseString(PWideChar(response), @i, cx_google_root);
       root:=parseString(PWideChar(response), @i, nil);
      except
       MessageBox(0, 'Parse error', piShortName, MB_OK or MB_ICONERROR);
       root:=0;
      end;
      if root<>0 then
       begin
        actnode:=getNthChild(root, cx_google_node, 0);
        if actnode=0 then
          MessageBox(0, 'Act node not found', piShortName, MB_OK or MB_ICONERROR);
        datanode:=getNthChild(actnode, cx_google_data, 0);
        if datanode=0 then
         begin
          MessageBox(0, 'Data node not found', piShortName, MB_OK or MB_ICONERROR);
          datastr:='';
         end
        else
         begin
          tmp:=getText(datanode);
          datastr:=WideCharToString(tmp);
         end;
        if datastr<>'' then
         begin
          pricestr:='';
          IsPrice:=false;
          for i:=0 to length(datastr)-1 do
           begin
            if datastr[i]+datastr[i+1]='= ' then IsPrice:=true;
            if (datastr[i+2]=' ') and not (datastr[i-1]+datastr[i]='= ') then
              IsPrice:=false;
            if IsPrice then
              pricestr:=pricestr+datastr[i+2];
           end;
          //
          DecimalSeparator:='.';
          price:=StrToFloat(pricestr);
          result.current.rate:=price;
          result.current.date:=cx_Today;
          result.success:=True;
         end;
       end
      else
        MessageBox(0, 'Zero root!', piShortName, MB_OK or MB_ICONEXCLAMATION);
     end;
     *)
end;
end.
